/*
 * Created by JFormDesigner on Mon Jul 01 15:13:05 CST 2024
 */

package com.hongyan.view.teacher;

import com.hongyan.base.BaseContentJpanel;
import com.hongyan.bean.Grades;
import com.hongyan.bean.GradesS_InfoDto;
import com.hongyan.config.MainFrameConfig;
import com.hongyan.dao.GradesDao;
import com.hongyan.dao.GradesS_InfoDao;
import com.hongyan.dao.HomeworkDao;
import com.hongyan.page.AbstractStatusBar;
import com.hongyan.page.PageInfo;
import com.hongyan.widget.ButtonEditor;
import com.hongyan.widget.ButtonRenderer;
import com.hongyan.widget.TableButtonClick;
import org.jdesktop.swingx.JXDatePicker;
import org.knowm.xchart.*;
import org.knowm.xchart.internal.chartpart.Chart;
import org.knowm.xchart.style.markers.SeriesMarkers;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Arrays;
import java.util.List;
import javax.swing.*;
import javax.swing.border.LineBorder;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.JTableHeader;

/**
 * @author admin
 */
public class SwingTeacherResultStatPanel extends BaseContentJpanel {


    public SwingTeacherResultStatPanel() {
        super();
        initData();
    }

    @Override
    public void initComponents() {
        setLayout(new BorderLayout());
        JPanel jPanel = new JPanel();
        jPanel.setLayout(new FlowLayout(FlowLayout.CENTER, 10, 10));
        PieChart chart = new PieChartBuilder().width(400).height(280).title("学生成绩分布图").build();
        initPieChart(chart);
        jPanel.add(chartPanel);
        chartPanel.setBounds(0, 0, 300, 200);
        CategoryChart chart2 = new CategoryChartBuilder().width(400).height(280).title("成绩分类人数").xAxisTitle("成绩分类").yAxisTitle("人数").build();
        initCategoryChart(chart2);
        jPanel.add(chartPanel2);
        chartPanel2.setBounds(0, 0, 300, 200);
        add(jPanel, BorderLayout.CENTER);

    }

    private void initCategoryChart(CategoryChart chart2) {
        List<GradesS_InfoDto> gradesSInfoDtos = GradesS_InfoDao.getGradesS_InfoDtos();
        List<String> categories = Arrays.asList("优秀", "良好", "中等", "及格", "不及格");
        int A1 = 0, B1 = 0, C1 = 0, D1 = 0, E1 = 0;
        int A2 = 0, B2 = 0, C2 = 0, D2 = 0, E2 = 0;
        for (GradesS_InfoDto gradesSInfoDto : gradesSInfoDtos) {
            double grade = Double.parseDouble(gradesSInfoDto.getGrade());
            if (grade >= 90) {
                if ("男".equals(gradesSInfoDto.getSex())) {
                    A1++;
                } else {
                    A2++;
                }
            } else if (grade >= 80) {
                if ("男".equals(gradesSInfoDto.getSex())) {
                    B1++;
                } else {
                    B2++;
                }
            } else if (grade >= 70) {
                if ("男".equals(gradesSInfoDto.getSex())) {
                    C1++;
                } else {
                    C2++;
                }
            } else if (grade >= 60) {
                if ("男".equals(gradesSInfoDto.getSex())) {
                    D1++;
                } else {
                    D2++;
                }
            } else {
                if ("男".equals(gradesSInfoDto.getSex())) {
                    E1++;
                } else {
                    E2++;
                }
            }
        }
        List<Number> seriesNanValues = Arrays.asList(A1, B1, C1, D1, E1);
        List<Number> seriesNvValues = Arrays.asList(A2, B2, C2, D2, E2);
        chart2.addSeries("男", categories, seriesNanValues);
        chart2.addSeries("女", categories, seriesNvValues);
        chartPanel2 = new XChartPanel<>(chart2);
    }

    private void initPieChart(PieChart chart) {
        List<Grades> gradesList = GradesDao.getGradesList();
        int size = gradesList.size();
        int A = 0, B = 0, C = 0, D = 0, E = 0;
        for (Grades grades : gradesList) {
            double grade = Double.parseDouble(grades.getGrade());
            if (grade > 90) {
                A++;
            } else if (grade > 80) {
                B++;
            } else if (grade > 70) {
                C++;
            } else if (grade > 60) {
                D++;
            } else {
                E++;
            }
        }

        chart.addSeries("优秀", ((double) A / size));
        chart.addSeries("良好", ((double) B / size));
        chart.addSeries("中等", ((double) C / size));
        chart.addSeries("及格", ((double) D / size));
        chart.addSeries("不及格", ((double) E / size));
        // 将图表面板添加到 JFrame
        chartPanel = new XChartPanel<>(chart);
    }

    @Override
    public void initData() {

    }

    @Override
    public void initListen() {

    }


    private XChartPanel chartPanel;
    private XChartPanel chartPanel2;
}
